package com.jzhao.jpre.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.jzhao.jpre.base.StringPublicDeal;
import com.jzhao.jpre.model.SubCode;

public class SubCodeDao {
	protected JdbcTemplate jdbcTemplate;
	protected StringPublicDeal strPubDeal;
	
	class SubCodeRowMapper implements RowMapper{
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException{
			SubCode subCode=new SubCode();
			subCode.setMrchCode(strPubDeal.trim(rs.getString("mrchCode")));
			subCode.setSubCode(strPubDeal.trim(rs.getString("subCode")));
			subCode.setTranName(strPubDeal.trim(strPubDeal.convToGB2312(rs.
					getString("tranName"))));
			subCode.setTranAtt(strPubDeal.trim(rs.getString("tranAtt")));
			subCode.setTranchar(strPubDeal.trim(rs.getString("tranChar")));
			subCode.setTranExplain(strPubDeal.trim(strPubDeal.convToGB2312(rs.
					getString("tranExplain"))));
			
			return subCode;
		}
	}
	
	public void setDataSource(DataSource dataSource){
		jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	public void setStrPubDeal(StringPublicDeal strPubDeal) {
		this.strPubDeal = strPubDeal;
	}
	
	public SubCode selectByKey(String mrchCode,String code)throws DataAccessException{
		SubCode subCode=null;
		String sql="SELECT * FROM t_jb__subcode WHERE mrchCode='" + mrchCode.trim() + "' AND SubCode='" + code.trim() + "'";
		subCode = (SubCode)jdbcTemplate.queryForObject(sql, new SubCodeRowMapper());
		return subCode;
	}
}
